import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import os,sys
sys.path.append(os.getcwd())
from selenium.webdriver.common.keys import Keys

class PageBase:
    def __init__(self,driver,base_url):
        self.driver=driver
        self.base_url=base_url



        #打开网站
    def open(self,path_url):
        self.driver.get(self.base_url+path_url)

        #By类定位元素
    def find(self,DW,gaoliang=False):
        by=DW[0]
        value=DW[1]
        ele=self.driver.find_element(by,value)
        if gaoliang:
            self.driver.execute_script('arguments[0].setAttribute(“style”,"color:red")', ele)
        return ele


        # By类定位元素多个
    def finds(self, DW):
        by = DW[0]
        value = DW[1]
        return self.driver.find_elements(by, value)

        #文本输入
    def input(self,DW,date):
        self.find(DW).send_keys(date)

        #清空再文本输入
    def input1(self, DW, date):
         ele=self.find(DW)
         ele.send_keys(Keys.CONTROL,'a','x')
         ele.send_keys(Keys.BACK_SPACE)
         ele.send_keys(date)


        #单选按钮
    def click_dx(self,typedate,namedate,valuedate):
        ele=self.driver.find_element(By.CSS_SELECTOR, f'input[type="{typedate}"][name="{namedate}"][value="{valuedate}"]')
        ele.click()

        # 复选按钮
    def click_fx(self,nameAttr,typeAttr,targetValues):
        alloption=self.driver.find_elements(By.CSS_SELECTOR,f'input[type="{typeAttr}"][name="{nameAttr}"]')
        for x in alloption:
            valueAttr=x.get_attribute('value')#获取属性值attr:attrbute属性
            status = x.is_selected()  # 判断单选按钮、复选框、下拉列表，是否是选中状态，status:状态
            if valueAttr in targetValues:#如果是目标
                if not status:#布尔取反操作
                    x.click()#如果没被选中
            else:#如果不是目标
                if status:#如果已被选中
                    x.click()

         # 下拉框

    def clicK_select(self, xlkDW, option, type='text'):  # type='text'默认文本
         xlDW = self.find(xlkDW)
         xl = Select(xlDW)
         if type == 'text':
            xl.select_by_visible_text(option)
         elif type == 'index':
            xl.select_by_index(option)
         elif type == 'value':
            xl.select_by_value(option)

    # 日期控件
    def input_date(self, rqDW, riqi):
        ele = self.find(rqDW)
        self.driver.execute_script('arguments[0].value=arguments[1]', ele, riqi)





        # 上传文件
    def upload_file(self, scDW, file):
        if file=='' or file==None:
            return
        ele = self.find(scDW)
        newfile = file.replace(',', '\n')
        ele.send_keys(newfile)

        #上传文件AUTOIT
    def upload_autoid(self,scDW,file):
        ele=self.find(scDW)
        ActionChains(self.driver).click(ele).perform()
        time.sleep(2)
        newfile=''
        a=file.split(',')
        for x in a:
            newfile+='"'+x+'"'
            newfile=newfile.replace('/','\\')
            a=os.getcwd().replace('\\','/')
            os.system(f'{a}/tools/uploadFile.exe{newfile}')

        #Js输入数据
    def js_input(self,DW,date):
        ele=self.find(DW)
        self.driver.execute_script('arguments[0].value=arguments[1]',ele,date)

        #js点击
    def js_click(self,DW):
        ele=self.find(DW)
        self.driver.execute_script('arguments[0].click()(',ele)

        #js修改属性值
    def js_changeattr(self,DW,attrname,attrvalue):
        ele=self.find(DW)
        self.driver.execute_script('arguments[0].setAttribute(arguments[1],arguments[2])',ele,attrname,attrvalue)

        #js删除shuxzh

    def js_delattr(self,DW,attrname):
        ele = self.find(DW)
        self.driver.execute_script('arguments[0].removeAttribute(arguments[1])', ele, attrname)

        #js修改样式表
    def js_seCssStyle(self, DW, cssstyle):
        ele = self.find(DW)
        self.driver.execute_script('arguments[0].setAttribute(“style”,arguments[1])', ele, cssstyle)

        #获取元素的文本内容或属性值  默认文本

    def getattr(self, DW,attrname='text'):
        ele=self.find(DW)
        if attrname=='text':
            return ele.text
        else:
            return ele.get_attribute(attrname)

        #鼠标单击
    def mouse_click(self,DW):
        ele=self.find(DW)
        ActionChains(self.driver).click(ele).perform()

        # 鼠标双击
    def mouse_doubleclick(self, DW):
            ele = self.find(DW)
            ActionChains(self.driver).doubleclick(ele).perform()

            # 鼠标双击
    def mouse_xuanting(self, DW):
            ele = self.find(DW)
            ActionChains(self.driver).move_to_element(ele).perform()

            #封装截图
    def jietu(self,name):
        #获取当前目录，拼接screen目录
        a = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../screen')
        currtime=time.strftime('%Y%m%d%H%M%S')
        Name=f'{name}_{currtime}'
        b = os.path.join(a, f'{Name}.png')
        self.driver.get_screenshot_as_file(b)
        return Name























